import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:redux/redux.dart';
import 'package:stock_check/common/model/Works.dart';
import 'package:stock_check/common/model/app.dart';
import 'package:stock_check/common/model/userInfo.dart';
import 'package:stock_check/common/redux/appReducer.dart';
import 'package:stock_check/page/loginPage.dart';
import 'package:stock_check/widget/CommonsWidget.dart';
import 'common/redux/reduxStore.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:stock_check/page/homePage.dart';
import 'package:connectivity/connectivity.dart';
import 'package:fluttertoast/fluttertoast.dart';

void main() {
  runApp(FlutterReduxApp(title: "设备管理"));
  if (Platform.isAndroid) {
    // 以下两行 设置android状态栏为透明的沉浸。写在组件渲染之后，是为了在渲染后进行set赋值，覆盖状态栏，写在渲染之前MaterialApp组件会覆盖掉这个值。
    SystemUiOverlayStyle systemUiOverlayStyle = SystemUiOverlayStyle(statusBarColor: Colors.transparent);
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }
}

class FlutterReduxApp extends StatelessWidget {
  final store = new Store<StockState>(appReducer,
      initialState: new StockState(
          userInfo: UserInfo.empty(),
          appInfo: App.empty(),
          worksInfo: Works.empty())
      );

  final String title;

  FlutterReduxApp({Key key, this.title}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return StoreProvider(
      store: store,
      child: StoreBuilder<StockState>(builder: (context,store){
        return MaterialApp(
          title: title,
          theme:CommonsWidget.getThemeDataByName(store.state.appInfo.themeType),
          debugShowCheckedModeBanner: false,
          routes: {
            "/": (context) {
              return Stock(
                child: HomePage(),
              );
            }
          },
        );
      })
    );
  }
}

class Stock extends StatefulWidget {
  final Widget child;

  const Stock({Key key, this.child}) : super(key: key);

  @override
  _StockState createState() => _StockState();
}

class _StockState extends State<Stock> {
  StreamSubscription subscription;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '资产管理',
      theme: Theme.of(context),
      debugShowCheckedModeBanner: false,
      home: widget.child,
      routes: {
        LoginPage.routeName:(context){
          return Stock(
            child: LoginPage(),
          );
        },
        HomePage.routeName: (context) {
          return Stock(
            child: HomePage(),
          );
        }
      },
    );
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    Store<StockState> store = StoreProvider.of(context);
    App appInfo = store.state.appInfo;
    //网络状况
    subscription = Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
      appInfo.netWork = result;
      if(result == ConnectivityResult.none){
        Fluttertoast.showToast(msg: "网络已断开!");
      }
      store.dispatch(UpdateAppAction(appInfo));
    });
  }

  @override
  void dispose() {
    subscription.cancel();
    super.dispose();
  }
}
